<?php

namespace App\Http\Controllers\Mini;

use App\Http\Resources\Mini\NoticeResource\NoticeClassCollection;
use App\Models\AdminEstate;
use App\Models\Notice;
use App\Services\NoticeService;
use Illuminate\Http\Request;

class NoticeController extends BaseController
{
    public $ownerId = 157;

    public function __constuct()
    {
//        $this->ownerId = 157;
    }

    public function index(Request $request, Notice $notice_model,NoticeService $noticeService)
    {
        /*$notice_model->where(['is_show' => 1, 'status' => 1]);
        if ($request->has("community_id")) {
            $notice_model->whereFindRaw("community_id", $request->get("community_id"));
        }
        $list = $notice_model->orderByDesc('id')->get();*/
        $pre_page = $request->pre_page;
        $list = $noticeService->gePageNotice($pre_page);
        if ($list['data']['total']>0) {
            $data = $list['data']['data'];
            foreach ($data as $k=>$v){
                $estate = $v['one_estate'];
                $data[$k]['author']=$estate['username'];//公告发布者
                $data[$k]['avatar']=$estate['avatar'];//公告发布者
            }
            $list['data']['data'] = $data;
        }
//        return $this->success(new NoticeClassCollection($list));
        return $list['status'] ? $this->success($list['data']) : $this->error('暂无数据');
    }

    /**公告详情
     * @param Notice $notice_model
     * @param $id
     * @return array
     */
    public function show(Notice $notice_model, $id)
    {
        $notice = $notice_model->find($id);
        $admin = AdminEstate::find($notice['estate_id']);
        $notice['name'] = $admin['name'] ?: "神秘人";
        $notice['avatar'] = $admin['avatar'] ?: "";
        return $this->success($notice);
    }
}
